//
//  ChangeGroupNameViewController.m
//  QinrenManagers
//
//  Created by yanxinlong on 15/9/14.
//  Copyright (c) 2015年 easaa. All rights reserved.
//

#import "ChangeGroupNameViewController.h"

@interface ChangeGroupNameViewController ()
{
    UITextField *groupName;
    
    UITextView *describView;
    
    NSString *_groupID;

}

@end

@implementation ChangeGroupNameViewController

- (instancetype)initWithGroup:(EMGroup *)groupEM {
    self = [super init];

    if (self) {
        _groupID = groupEM.groupId;
    }
    
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = Lightlightcolor;
    [self creatUI];

}

- (void)creatUI {

    groupName = [[UITextField alloc]initWithFrame:CGRectMake(10, 80, SCREEN_W-20, 60)];
    
    groupName.placeholder = @"新的群名称";
    groupName.layer.borderColor=[[UIColor redColor]CGColor];
    groupName.borderStyle = UITextBorderStyleRoundedRect;
    [self.view addSubview: groupName];
    
    describView = [[UITextView alloc]initWithFrame:CGRectMake(10, CGRectGetMaxY(groupName.frame)+20, SCREEN_W-20, 150)];
    describView.text = @"群描述:";
    describView.font = [UIFont systemFontOfSize:14];
    [self.view addSubview: describView];
    
    
    UIButton *changeButton = [[UIButton alloc]initWithFrame:CGRectMake(SCREEN_W/2-100, CGRectGetMaxY(describView.frame)+20, 200, 50)];
    changeButton.titleLabel.font = [UIFont systemFontOfSize:20];
    [changeButton setTitle:@"修改" forState:UIControlStateNormal];
    [changeButton setBackgroundColor:DarkGreencolor];
    [changeButton addTarget:self action:@selector(changeInfomation) forControlEvents:UIControlEventTouchUpInside];
    changeButton.layer.cornerRadius = 5.0;
    changeButton.clipsToBounds = YES;
    [self.view addSubview: changeButton];
    
}


- (void)changeInfomation {
    
    if (groupName.text.length==0) {
        [SVProgressHUD showErrorWithStatus:@"群名称不能为空"];
    }
    else {
        EAHttpAPIClient *manager = [EAHttpAPIClient manager];
        manager.responseSerializer = [AFHTTPResponseSerializer serializer];
        
        NSDictionary *parameters = @{@"groupid":_groupID,
                                     @"groupname":groupName.text,
                                     @"desc":describView.text};
        NSLog(@"%@",parameters);
        [manager GetWithParameters:parameters method:@"update.hxgroup.info" success:^(AFHTTPRequestOperation *operation, id responseObject) {
            NSString *resposeString ;
            if ([responseObject isKindOfClass:[NSData class]]) {
                resposeString = [[NSString alloc]initWithData:responseObject encoding:NSUTF8StringEncoding];
            }else
                resposeString = responseObject;
            
            NSString *jsonString = [DataParser parseString:resposeString];
            id dictionary = [JSONKit jsonToArrayOrDictionary:jsonString];
            NSLog(@"修改群名的dictonary :%@",[dictionary description]);
            
            
            if ([[dictionary objectForKey:@"verification"]intValue] == 1)
            {
                
                if ([dictionary[@"total"] integerValue]==0) {
                    [SVProgressHUD showSuccessWithStatus:@"修改成功"];
                }
                
            }
            else
            {
                
                NSString *error = [dictionary objectForKey:@"error"];
                [SVProgressHUD showWithMaskType:SVProgressHUDMaskTypeClear];
                [SVProgressHUD showErrorWithStatus:error];
            }
            
        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
            
            [SVProgressHUD showWithMaskType:SVProgressHUDMaskTypeClear];
            [SVProgressHUD showErrorWithStatus:[error localizedDescription]];
            
        }];
    }
}

@end
